package com.yellowpage.bigdata.service;

import java.util.List;
import java.util.Map;

import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;
import com.yellowpage.bigdata.data.ActiveWeek;
import com.yellowpage.bigdata.dao.ActiveWeekDao;

public class ActiveWeekServiceImpl extends BaseServiceImpl implements
		ActiveWeekService {
	private ActiveWeekDao activeWeekDao;

	public void setActiveWeekDao(ActiveWeekDao activeWeekDao) {
		this.activeWeekDao = activeWeekDao;
	}

	/* Smooth generate Method */
	public ActiveWeek getActiveWeek(Long id) {
		return activeWeekDao.get(id);
	}

	public List<ActiveWeek> getAllActiveWeek() {
		return activeWeekDao.getAll();
	}

	public List<Map> queryActiveWeekByPage(Map queryMap, PageRender pageRender) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT ROUND(b.RATE*100,2)RATE,a.VERSION,a.DAY_ID,a.CNTS,a.CHANNEL_NO,(SELECT `NAME` FROM OMC_DIC_TABLE d WHERE d.`VALUE` = a.CHANNEL_NO AND d.DIC_NAME = 'NSP_ONOFFLOG_CHANNEL_NO') CHANNEL_NO_NAME,ROUND(a.CNTS*100 / c.sam,2) PROP FROM TM_APPONOFF_ACTIVE_WEEK a,TM_APPONOFF_ACTIVE_RATE_WEEK b,(select sum(cnts) sam from TM_APPONOFF_ACTIVE_WEEK d1,TM_APPONOFF_ACTIVE_RATE_WEEK d2 WHERE d1.DAY_ID = d2.DAY_ID and d1.VERSION = d2.VERSION and d1.CHANNEL_NO = d2.CHANNEL_NO");
		if (!"".equals(queryMap.get("day_id"))) {
			sql.append(" AND d1.DAY_ID >=" + queryMap.get("day_id"));
		}
		if (!"".equals(queryMap.get("day_id_end"))) {
			sql.append(" AND d1.DAY_ID <=" + queryMap.get("day_id_end"));
		}
		if (!"".equals(queryMap.get("version"))) {
			sql.append(" AND d1.VERSION = '" + queryMap.get("version") + "'");
		}
		if (!"".equals(queryMap.get("channelNo"))) {
			sql.append(" AND d1.CHANNEL_NO = '" + queryMap.get("channelNo")
					+ "'");
		}
		sql.append(") c WHERE a.DAY_ID = b.DAY_ID AND a.VERSION = b.VERSION AND a.CHANNEL_NO = b.CHANNEL_NO");
		if (!"".equals(queryMap.get("day_id"))) {
			sql.append(" AND a.DAY_ID >=" + queryMap.get("day_id"));
		}
		if (!"".equals(queryMap.get("day_id_end"))) {
			sql.append(" AND a.DAY_ID <=" + queryMap.get("day_id_end"));
		}
		if (!"".equals(queryMap.get("version"))) {
			sql.append(" AND a.VERSION = '" + queryMap.get("version") + "'");
		}
		if (!"".equals(queryMap.get("channelNo"))) {
			sql.append(" AND a.CHANNEL_NO = '" + queryMap.get("channelNo")
					+ "'");
		}
		QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(sql
				.toString());

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}

	public void saveActiveWeek(ActiveWeek activeWeek) {
		activeWeekDao.save(activeWeek);
	}

	public void updateActiveWeek(ActiveWeek activeWeek) {
		activeWeekDao.update(activeWeek);
	}

	public void delete(List deleteList) {
		activeWeekDao.remove(deleteList);
	}

}